/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { WorkerPoolImpl } from '../../../../../src/base/internal/pool/worker-pool/worker-pool-impl';
import { PoolConfig } from '../../../../../src/base/internal/pool/worker-pool/pool-config';
import { PoolState } from '../../../../../src/base/internal/pool/enum/pool-state';
import { Work } from '../../../../../src/base/internal/pool/model/work';
import { Priority } from '../../../../../src/base/internal/pool/enum/priority';
import { TaskState } from '../../../../../src/base/internal/pool/enum/task-state';
import { TCB } from '../../../../../src/base/internal/pool/model/tcb';
import { TaskExecuteStatus } from '../../../../../src/base/internal/task/core/task-execute-status';

describe('test worker pool impl', () => {
  let workerPool: any;
  const poolConfig: PoolConfig = {
    maxPoolNum: 2
  };
  test('test getInstance', () => {
    workerPool = WorkerPoolImpl.getInstance(poolConfig);
    expect(workerPool).toBeDefined();
  });

  test('test setState', () => {
    workerPool.setState(PoolState.TERMINATED);
    expect(workerPool.getState() === PoolState.RUNNING);
    workerPool.setState(PoolState.CLOSE);
    expect(workerPool.getState() === PoolState.CLOSE);
    workerPool.setState(PoolState.RUNNING);
    expect(workerPool.getState() === PoolState.RUNNING);
  });

  test('test submitWork', () => {
    workerPool.setState(PoolState.CLOSE);
    const work = new Work(Priority.FIRST, undefined, '');
    const a: any = {
      taskExecutedStatus: new TaskExecuteStatus(),
      getNode: () => {
        return {
          getName: () => {
            return '';
          }
        };
      },
      getName: () => {
        return '';
      },
      getPath: () => {
        return '';
      },
      addSubMetric: (workId: string) => {
        //
      }
    };
    expect(workerPool.submitWork(a, work).getState() === TaskState.REJECT).toBeTruthy();
    workerPool.setState(PoolState.RUNNING);
    expect(workerPool.submitWork(a, work).getState() === TaskState.RUNNING).toBeTruthy();
  });

  test('test terminate', () => {
    workerPool.terminate().then((res: boolean) => {
      expect(res).toBeTruthy();
    });
  });
});
